﻿using System;
using System.ComponentModel;
using System.Diagnostics;

namespace Songhay.Silverlight.Mvvm.ViewModels
{
    /// <summary>
    /// Lightweight View Model designed for items in collections.
    /// </summary>
    public class TinyViewModelBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Gets a value indicating whether this instance is in design mode.
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is in design mode; otherwise, <c>false</c>.
        /// </value>
        public bool IsInDesignMode
        {
            get
            {
                if(!isInDesignMode.HasValue)
                {
                    isInDesignMode = DesignerProperties.IsInDesignTool;
                }
                return isInDesignMode.Value;
            }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void RaisePropertyChanged(string propertyName)
        {
            this.VerifyPropertyName(propertyName);

            var handler = PropertyChanged;
            if(handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        [Conditional("DEBUG")]
        [DebuggerStepThrough]
        void VerifyPropertyName(string propertyName)
        {
            if(this.GetType().GetProperty(propertyName) != null) return;

            throw new ArgumentException("Property not found", propertyName);
        }

        static bool? isInDesignMode;
    }
}
